﻿using System;
using System.Collections.Generic;
using System.Web;

using Gift.Model;
using Gift.Utile;
using Gift.DAL;
using System.Text.RegularExpressions;

namespace MvcSite.Service
{
    public class BasePage : System.Web.UI.Page
    {
        readonly DALProfessionAreaType theProfessionAreaType = new DALProfessionAreaType();
        tbProfessionAreaType oProfessionAreaType;
        public string ClientIP
        {
            get
            {
                string userIP;
                if (Request.ServerVariables["HTTP_VIA"] == null)
                {
                    userIP = Request.UserHostAddress;
                }
                else
                {
                    userIP = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                }
                return userIP;

            }
        }



        public int CompanyId
        {
            get
            {

                return 1;

            }

        }


        public Dictionary<int, decimal> DicProductPrice
        {
            get
            {
                if (Session["DicProductPrice"] == null)
                {
                    return null;
                }
                else
                {
                    return Session["DicProductPrice"] as Dictionary<int, decimal>;
                }
            }
            set { Session["DicProductPrice"] = value; }
        }




        public UserMember LoginUserMember
        {
            get
            {
                if (Session["UserMember"] != null)
                {
                    return Session["UserMember"] as UserMember;
                }
                else
                {
                    return null;
                }
            }
            set
            {
                Session["UserMember"] = value;
            }
        }

        public string ServerImagePath
        {
            get
            {
                return System.Configuration.ConfigurationManager.AppSettings["ProductUrl"];
            }
        }


        public string SetProductPrice()
        {
            string priceMessage = string.Empty;
            if (DicProductPrice == null || DicProductPrice.Count <= 0)
            {
                priceMessage = "您没有选取商品！";
                return priceMessage;
            }
            if (DicProductPrice != null && LoginUserMember.DicUserCardList != null)
            {
                Dictionary<int, decimal> dicCards = new Dictionary<int, decimal>();
                foreach (tbCard oCard in LoginUserMember.DicUserCardList.Values)
                {
                    oProfessionAreaType = theProfessionAreaType.GetModelByCardTypeId(oCard.cardtypeId);
                    dicCards[oProfessionAreaType.Id] = dicCards.ContainsKey(oProfessionAreaType.Id) ? dicCards[oProfessionAreaType.Id] + oCard.cardprice : oCard.cardprice;
                }


                if (DicProductPrice.Count != dicCards.Count)
                {
                    priceMessage = "所选商品与卡券不能对应！";
                    return priceMessage;
                }
                foreach (int key in DicProductPrice.Keys)
                {
                    if (dicCards != null && dicCards[key] != DicProductPrice[key])
                    {
                        priceMessage = "所选商品与卡券不能对应！";
                        return priceMessage;
                    }
                }

            }

            return string.Empty;
        }


    }

    [Serializable]
    public class UserMember
    {
        string _memberId = string.Empty;

        public string MemberId
        {
            get { return _memberId; }
            set { _memberId = value; }
        }

        //string _memberName = string.Empty;

        //public string MemberName
        //{
        //    get { return _memberName; }
        //    set { _memberName = value; }
        //}

        bool _isCard = false;

        public bool IsCard
        {
            get { return _isCard; }
            set { _isCard = value; }
        }

        Dictionary<int, tbCard> _dicUserCardList;

        public Dictionary<int, tbCard> DicUserCardList
        {
            get { return _dicUserCardList; }
            set { _dicUserCardList = value; }
        }

        tbUser _clientUser;

        public tbUser ClientUser
        {
            get { return _clientUser; }
            set { _clientUser = value; }
        }



        //decimal _accountMoney = 0;

        //public decimal AccountMoney
        //{
        //    get { return _accountMoney; }
        //    set { _accountMoney = value; }
        //}



        public virtual void Dispose()
        {
        }

        //public override void Init(HttpApplication app)
        //{
        //    app.AuthorizeRequest += new EventHandler(this.BaseModuleRewriter_AuthorizeRequest);
        //}

        //  protected abstract void Rewrite(string requestedPath, HttpApplication app);

    }
}